class DataController < ApplicationController

	
	def invitation
		if params[:uploaded_data]
			@uploaded_data = params[:uploaded_data]
			names = params[:uploaded_data].read.split("\n")
			
			@result = process_names(names)		
		else	
			@result = Array.new
			@names = params[:name].values
			@names = @names.map { |name| name unless name.blank? }
			@names.compact!
			counter = 0			
		end		
	end	
	
	def addresses
		if params[:uploaded_data]
			@uploaded_data = params[:uploaded_data]
			addresses = params[:uploaded_data].read.split("\n\n")
			
			@result = process_addresses(addresses)		
		else	
			@result = Array.new
			@addresses = params[:name].values
			@addresses = @names.map { |name| name unless name.blank? }
			@addresses.compact!
			counter = 0			
		end		
	end		
	
	def process_names(names)	
		counter = 0
		result = []
		names.in_groups_of(2) do |name|
		counter = counter + 1
		result << "<v:sampleDataSet  dataSetName=\"#{counter}\">
				<Name1>
					<p>" + name[0].titleize + "</p>
				</Name1>
				<Name2>
					<p>"
					result << name[1].titleize + "</p></Name2></v:sampleDataSet>" unless name[1].nil?	
					result << "</p></Name2></v:sampleDataSet>" if name[1].nil?
		end
	result
	end


	def process_addresses(names)	
		counter = 0
		result = []
		names.in_groups_of(1) do |name|
		counter = counter + 1
		result << "<v:sampleDataSet  dataSetName=\"#{counter}\">
				<Name1>"
				splitnames = name[0].split("\n")
				splitnames.each do |splitname|
					result << "<p>" + splitname + "</p>"
				end
				result << "</Name1></v:sampleDataSet>"
		end
	result
	end
end